package com.gavin.export.utils;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;

public class SignMd5Util {

    /*
     * 生成随机字符串的长度
     */
    private static final String lents = "8";

    /**
     * 截取字符串开始长度
     */
    private static final String star = "0";

    /**
     * 截取字符串结束
     */
    private static final String end = "8";


    private static String clientId = "";

    /**
     * 获取加密秘钥
     *
     * @param timeMd5
     * @return
     */
    public static JSONObject getDuiChenSign(@RequestParam(value = "id", required = false) String id,
                                            @RequestParam(value = "timeMd5", required = false) String timeMd5) {
        String newtimeMd5 = null;
        // ==================== 时间md5 使用方存储 =================
        // 生成当前时间戳
        long nanoTime = System.nanoTime();
        // 生成指定长度的字符串
        int len = Integer.parseInt(lents);
        String str = SuijishuUtil.getStr(len);
        StringBuffer sb = new StringBuffer();
        sb.append(nanoTime);
        int starts = Integer.parseInt(star);
        int ends = Integer.parseInt(end);
        if (!(starts <= str.length()) && !(ends >= str.length()) && starts < ends) {
            sb.append(str.substring(starts, ends));
        }
        newtimeMd5 = MD5Utils.getMD5(sb.toString());
        // ================== 时间加密戳结束

        if (ObjectUtils.isEmpty(timeMd5)) {
            // 用户首次进入仅需要加密
            timeMd5 = newtimeMd5;
        }
        String oldidmd5 = null;
        String newidmd5 = null;
        if (ObjectUtils.isEmpty(id)) {
            // 生成的id
            oldidmd5 = MD5Utils.getMD5(timeMd5);

            newidmd5 = MD5Utils.getMD5(newtimeMd5);
        } else {
            oldidmd5 = newidmd5 = MD5Utils.getMD5(id);
        }

        // 生成的加密串 加密串 = (idmd5 + newtimeMd5) 的 md5
        String encodemd5 = MD5Utils.getMD5(newidmd5 + newtimeMd5);

        // 解密串
        String decodeMd5 = MD5Utils.getMD5(oldidmd5 + timeMd5);

        JSONObject json = new JSONObject();
        json.put("decodeMd5", decodeMd5);
        json.put("timeMd5", newtimeMd5);
        json.put("encodeMd5", encodemd5);
        return json;
    }


    /**
     * 获取加密秘钥
     *
     * @param timeMd5
     * @return
     */
    public JSONObject getDuiChenSignMast(@RequestParam(value = "id", required = false) String id,
                                         @RequestParam(value = "timeMd5", required = false) String timeMd5) {
        String newtimeMd5 = null;
        // ==================== 时间md5 使用方存储 =================
        // 生成当前时间戳
        long nanoTime = System.nanoTime();
        // 生成指定长度的字符串
        int len = Integer.parseInt(lents);
        String str = SuijishuUtil.getStr(len);
        StringBuffer sb = new StringBuffer();
        sb.append(nanoTime);
        int starts = Integer.parseInt(star);
        int ends = Integer.parseInt(end);
        if (!(starts <= str.length()) && !(ends >= str.length()) && starts < ends) {
            sb.append(str.substring(starts, ends));
        }
        newtimeMd5 = MD5Utils.getMD5(sb.toString());
        // ================== 时间加密戳结束

        if (ObjectUtils.isEmpty(timeMd5)) {
            // 用户首次进入仅需要加密
            timeMd5 = newtimeMd5;
        }
        String oldidmd5 = null;
        String newidmd5 = null;
        if (ObjectUtils.isEmpty(id)) {
            // 生成的id
            oldidmd5 = MD5Utils.getMD5(timeMd5);

            newidmd5 = MD5Utils.getMD5(newtimeMd5);
        } else {
            oldidmd5 = newidmd5 = MD5Utils.getMD5(id);
        }

        // 生成的加密串 加密串 = (idmd5 + newtimeMd5) 的 md5
        String encodemd5 = MD5Utils.getMD5(newidmd5 + newtimeMd5);

        // 解密串
        String decodeMd5 = MD5Utils.getMD5(oldidmd5 + timeMd5);

        JSONObject json = new JSONObject();
        json.put("decodeMd5", decodeMd5);
        json.put("timeMd5", newtimeMd5);
        json.put("encodeMd5", encodemd5);
        return json;
    }


    public static JSONObject getMd5Message(String md5) {
        JSONObject duiChenSign = SignMd5Util.getDuiChenSign(StringUtils.isBlank(clientId) ? null : clientId,
                StringUtils.isBlank(md5) ? null : md5);
        return duiChenSign;
    }

}
